package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.util.ValidateCodeUtils;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UserInfoController
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-08-29 18:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController {

    @Reference
    private UserInfoService userInfoService;

    //post('/userInfo/register',this.registerVo)
    @RequestMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo, HttpServletRequest request){
        String nickName = registerVo.getNickName();
        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //检验参数
        if (StringUtils.isEmpty(nickName) || StringUtils.isEmpty(phone) || StringUtils.isEmpty(password) || StringUtils.isEmpty(code)){
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }

        //验证码
        String currentCode = (String)request.getSession().getAttribute("CODE");
        if (!code.equals(currentCode)){
            return Result.build(null,ResultCodeEnum.CODE_ERROR);
        }
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if (userInfo != null) {
            return Result.build(null,ResultCodeEnum.PHONE_REGISTER_ERROR);
        }

        userInfo = new UserInfo();
        userInfo.setNickName(nickName);
        userInfo.setPhone(phone);
        userInfo.setPassword(MD5.encrypt(password));
        userInfo.setStatus(1);
        userInfoService.insert(userInfo);
        return Result.ok();
    }

    //get('/userInfo/sendCode/'+ this.registerVo.phone)
    @RequestMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable String phone,HttpServletRequest request){
        //生成验证码
        String validateCode = ValidateCodeUtils.generateValidateCode4String(6);
        //通过阿里短信发送到指定的手机（此处略）
        //把验证码保存在session中，注册时进行比较
        request.getSession().setAttribute("CODE",validateCode);
        //返回验证码
        return Result.ok(validateCode);
    }

    //post('/userInfo/login',this.loginVo)
    @RequestMapping("/login")
    public Result login(@RequestBody LoginVo loginVo,HttpServletRequest request){
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();

        //校验参数
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)){
            return Result.build(null,ResultCodeEnum.PARAM_ERROR);
        }

        UserInfo userInfo = userInfoService.getByPhone(phone);
        if (userInfo == null) {
            return Result.build(null,ResultCodeEnum.ACCOUNT_ERROR);
        }

        //校验密码
        if (!MD5.encrypt(password).equals(userInfo.getPassword())){
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }

        //校验是否被禁用
        if (userInfo.getStatus() == 0){
            return Result.build(null,ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        request.getSession().setAttribute("USER",userInfo);
        Map<String,Object> map = new HashMap<>();
        //map.put("phone",userInfo.getPhone());
        map.put("nickName",userInfo.getNickName());
        return Result.ok(map);
    }

    @RequestMapping("/logout")
    public Result logout(HttpServletRequest request){
        request.getSession().removeAttribute("USER");
        return Result.ok();
    }
}
